package com.rg.model;

import java.util.*;
//代表一个订单，包含订单ID、总价、商品总数、状态、支付方式、收货人姓名、电话、地址、下单时间以及关联的用户信息。
//包含一个Map来存储订单项（listmap），键为商品ID，值为OrderItemsModel对象。
//提供了添加商品、减少商品和删除商品的方法。
public class OrderModel {
    private Integer id;
    private double total;//总价
    private Integer amount;// 商品总数
    private Integer status;//1未付款/2已付款/3已发货/4已完成
    private Integer paytype;//1微信/2支付宝/3货到付款
    private String name;
    private String phone;
    private String address;
    private Date datetime;
    private UserModel user;
    private Map<Integer,OrderItemsModel> listmap=new HashMap<>();
    private List<OrderItemsModel> orderitem=new ArrayList<>();
    //添加购物车的方法
    public void addGoods(GoodsModel goods,Integer addnum) {
        //先判断，已有的订单项是否有相同的，如果有，只用添加数量+1即可
        if(listmap.containsKey(goods.getGoodsId())) {
            OrderItemsModel item = listmap.get(goods.getGoodsId());
            item.setAmount(item.getAmount()+addnum);
        }else {
            amount=0;
            OrderItemsModel item = new OrderItemsModel(goods.getGoodsPrice(),addnum,goods,this);
            listmap.put(goods.getGoodsId(), item);
        }
        amount+=addnum;
        //将已有的总价加上新加的价格
        total += goods.getGoodsPrice()*addnum;
        System.out.println("addGoods中的amount"+amount);
        System.out.println("addGoods中的total"+total);

    }
    //减少商品
    public void lessonGoods(Integer goodsId) {
        if(listmap.containsKey(goodsId)) {
            OrderItemsModel goodsitem = listmap.get(goodsId);
            goodsitem.setAmount(goodsitem.getAmount()-1);
            amount--;
            total =total-goodsitem.getPrice();
            if(goodsitem.getAmount()<=0) {
                listmap.remove(goodsId);
            }
        }
    }
    //删除商品
    public void deleteGoods(Integer goodsId){
        if (listmap.containsKey(goodsId)) {
            OrderItemsModel orderItem = listmap.get(goodsId);
            total = total - orderItem.getAmount() * orderItem.getPrice();
            amount = 0;
            listmap.remove(goodsId);
        }
    }
    public OrderModel() {
    }

    public OrderModel(Integer id, double total, Integer amount, Integer status, Integer paytype, String name, String phone, String address, Date datetime, UserModel user, Map<Integer, OrderItemsModel> listmap, List<OrderItemsModel> orderitem) {
        this.id = id;
        this.total = total;
        this.amount = amount;
        this.status = status;
        this.paytype = paytype;
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.datetime = datetime;
        this.user = user;
        this.listmap = listmap;
        this.orderitem = orderitem;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getPaytype() {
        return paytype;
    }

    public void setPaytype(Integer paytype) {
        this.paytype = paytype;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getDatetime() {
        return datetime;
    }

    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }

    public UserModel getUser() {
        return user;
    }

    public void setUser(UserModel user) {
        this.user = user;
    }

    public Map<Integer, OrderItemsModel> getListmap() {
        return listmap;
    }

    public void setListmap(Map<Integer, OrderItemsModel> listmap) {
        this.listmap = listmap;
    }

    public List<OrderItemsModel> getOrderitem() {
        return orderitem;
    }

    public void setOrderitem(List<OrderItemsModel> orderitem) {
        this.orderitem = orderitem;
    }
}

